package 线程;

public class 守护线程 {
    public static void main(String[] args) throws InterruptedException {
        Daemon daemon = new Daemon();
        daemon.setDaemon(true);//设置线程为守护线程，主线程执行完成后，守护线程也结束
        daemon.start();

        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            System.out.println("主线程" + i);
        }
    }
}

class Daemon extends Thread{
    @Override
    public void run() {
        for(;;){
            try {
                Thread.sleep(1000);
                System.out.println("子线程");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}